// index.js
Page({
  data: {
    swiperList: [] ,
    catItems: [],
    floorList: [],
  },
  onLoad: function() {
    this.getSwiperData();
    this.fetchCatItems();
    this.getFloorData();
  },
  getSwiperData: function() {
    var that = this;
    wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata',
      method: 'GET',
      success: function(res) {
        if (res.data.meta.status === 200) {
          that.setData({
            swiperList: res.data.message
          });
        }
      }
    });
  },
  fetchCatItems: function() {
    var that = this;
    wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/catitems',
      method: 'GET',
      success: function(res) {
        if (res.data.meta.status === 200) {
          that.setData({
            catItems: res.data.message
          });
        }
      },
      fail: function(err) {
        console.error(err);
      }
    });
  },
  getFloorData: function(){
    var that = this;
    wx.request({
      url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/floordata',
      method: 'GET',
      success: function(res) {
        if (res.data.meta.status === 200) {
          that.setData({
            floorList: res.data.message
          });
        }
      }
    });
  },
  goToSearchPage: function() {
    wx.navigateTo({
      url: '/pages/search/search'
    });
  },
  onGoodsDetail: function(event) {
    const gid = event.currentTarget.dataset.gid;
    console.log('onGoodsDetail gid:', gid);  
    wx.navigateTo({
      url: `/pages/goodsdetails/goodsdetails?gid=${gid}`
    });
  }
})
